/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.mt.roll.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Resource;
import com.mt.roll.core.dal.entity.Service;
import com.mt.roll.core.dal.service.ResourceServiceBiz;
import com.mt.roll.core.dal.service.ServiceServiceBiz;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.upms.params.ServiceParam;
import com.mt.roll.upms.params.ServiceQueryParam;
import com.mt.roll.upms.service.MicroServiceService;
import com.mt.roll.upms.vo.ServiceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 微服务服务实现类
 *
 * @author jiff
 * @date 2018/11/23
 * @since 1.0
 */
@org.springframework.stereotype.Service
@Transactional
public class MicroServiceServiceImpl extends BaseService implements MicroServiceService {
    @Autowired
    private ServiceServiceBiz serviceServiceBiz;
    @Autowired
    private ResourceServiceBiz resourceServiceBiz;

    /**
     * 获取服务
     *
     * @param idParam 主键参数
     * @return 服务
     */
    @Override
    public R<ServiceVo> get(IdParam idParam) {
        Service service = serviceServiceBiz.getById(idParam.getId());
        Assert.notNull("服务不存在", service);
        ServiceVo serviceVo = ServiceVo.builder().build();
        BeanUtils.copyProperties(service, serviceVo);
        return successful(serviceVo);
    }

    /**
     * 获取服务列表
     *
     * @param serviceQueryParam 服务查询参数
     * @return 服务列表
     */
    @Override
    public R<List<ServiceVo>> list(ServiceQueryParam serviceQueryParam) {
        QueryWrapper<Service> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(serviceQueryParam.getEnabled()), Service.ENABLED, serviceQueryParam.getEnabled())
                .like(StringUtils.isNotBlank(serviceQueryParam.getName()), Service.NAME, serviceQueryParam.getName())
                .like(StringUtils.isNotBlank(serviceQueryParam.getCode()), Service.CODE, serviceQueryParam.getCode());
        List<Service> services = serviceServiceBiz.list(wrapper);
        List<ServiceVo> serviceVos = com.mt.roll.core.utils.BeanUtils.copyList(services, ServiceVo.class);
        return successful(serviceVos);
    }

    /**
     * 获取服务分页列表
     *
     * @param serviceQueryParam 服务查询参数
     * @return 服务分页列表
     */
    @Override
    public R<PageVo<ServiceVo>> page(ServiceQueryParam serviceQueryParam) {
        QueryWrapper<Service> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(serviceQueryParam.getEnabled()), Service.ENABLED, serviceQueryParam.getEnabled())
                .like(StringUtils.isNotBlank(serviceQueryParam.getName()), Service.NAME, serviceQueryParam.getName())
                .like(StringUtils.isNotBlank(serviceQueryParam.getCode()), Service.CODE, serviceQueryParam.getCode());
        IPage<Service> page = serviceServiceBiz.page(new Page<>(serviceQueryParam.getCurrent(), serviceQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, ServiceVo.class));
    }

    /**
     * 新增服务
     *
     * @param serviceParam 服务参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(ServiceParam serviceParam) {
        Service service = new Service();
        BeanUtils.copyProperties(serviceParam, service);
        setCommonField(service);
        serviceServiceBiz.save(service);
        return successful(IdVo.builder().id(service.getId()).build());
    }

    /**
     * 更新服务
     *
     * @param serviceParam 服务参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(ServiceParam serviceParam) {
        Service service = serviceServiceBiz.getById(serviceParam.getId());
        Assert.notNull("服务不存在", service);
        Service updateService = new Service();
        BeanUtils.copyProperties(serviceParam, updateService);
        setUpdateCommonField(updateService);
        serviceServiceBiz.updateById(updateService);
        return successful(IdVo.builder().id(service.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        Service service = serviceServiceBiz.getById(enabledParam.getId());
        Assert.notNull("服务不存在", service);
        Service updateService = new Service().setId(enabledParam.getId());
        updateService.setEnabled(enabledParam.getEnabled());
        setUpdateCommonField(updateService);
        serviceServiceBiz.updateById(updateService);
        return successful(IdVo.builder().id(service.getId()).build());
    }

    /**
     * 删除服务
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        Service service = serviceServiceBiz.getById(idParam.getId());
        Assert.notNull("服务不存在", service);
        Assert.eqZero(resourceServiceBiz.count(new QueryWrapper<Resource>().eq(Resource.SERVICE_ID, idParam.getId())), "该服务下还有资源");
        serviceServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(service.getId()).build());
    }

    /**
     * 批量删除服务
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }
}
